//
//  BootPageViewController.m
//  YiTangZuoWenKe
//
//  Created by Work on 17/4/8.
//  Copyright © 2017年 wyp. All rights reserved.
//引导页VC

#import "BootPageViewController.h"
#import "TabBarViewController.h"

@interface BootPageViewController ()<UIScrollViewDelegate>
/**判断是否是第一次进入应用*/
@property(nonatomic,assign)BOOL flag;
@property(nonatomic,assign) NSTimer *timer;
@property(nonatomic,strong) UIScrollView *myScrollView;
@property(nonatomic,strong) UIPageControl *pageControl;

@end
@implementation BootPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, KSCREEWIDTH, KSCREEHEGIHT)];
    NSString *iconNameStr = @"";
    if (iPhone5) {
        iconNameStr = @"appStart5_";
    }else if (iPhone6){
        iconNameStr = @"appStart6_";
    }else if (iPhone6P){
        iconNameStr = @"appStart6p_";
    }else if (iPhoneX){
        iconNameStr = @"appStartx_";
    }else if (iPhoneXR){
        iconNameStr = @"appStartxr_";
    }else if (iPhoneXSM){
        iconNameStr = @"appStartxsm_";
    }else{//iPhone4
        iconNameStr = @"appStart4s_";
    }
    for (int i=0; i<3; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%d.jpg",iconNameStr,i+1]];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(KSCREEWIDTH * i, 0, KSCREEWIDTH, KSCREEHEGIHT)];
        // 在最后一页创建按钮
        if (i == 2) {
            // 必须设置用户交互 否则按键无法操作
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            if (KSCREEWIDTH ==320) {
                btn.frame=CGRectMake(KSCREEWIDTH/2-65, KSCREEHEGIHT-80,130,34);
            }
            if (KSCREEWIDTH==375) {
                btn.frame=CGRectMake(KSCREEWIDTH/2-65, KSCREEHEGIHT-90,130,34);
            }
            if (KSCREEWIDTH==414) {
                btn.frame=CGRectMake(KSCREEWIDTH/2-65, KSCREEHEGIHT-100,130,34);
            }
           [btn addTarget:self action:@selector(clickBtn) forControlEvents:(UIControlEventTouchUpInside)];
            btn.backgroundColor = [UIColor redColor];
            [btn setTitle:@"立即体验" forState:UIControlStateNormal];
            imageView.userInteractionEnabled = YES;
            [imageView addSubview:btn];
        }
        imageView.image = image;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        [self.myScrollView addSubview:imageView];
    }
    self.myScrollView.bounces = NO;
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.showsHorizontalScrollIndicator = NO;
    self.myScrollView.contentSize = CGSizeMake(KSCREEWIDTH * 3, KSCREEHEGIHT);
    self.myScrollView.delegate = self;
    [self.view addSubview:self.myScrollView];
    
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 40, [UIScreen mainScreen].bounds.size.width, 40)];
    [self.view addSubview:self.pageControl];
    self.pageControl.numberOfPages = 3;
    self.pageControl.currentPage = 0;
    self.pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    


    [self addTimer];
}

-(void)clickBtn{
    _flag = YES;
    NSUserDefaults *useDef = [NSUserDefaults standardUserDefaults];
    // 保存用户数据
    [useDef setBool:_flag forKey:@"notFirst"];
    [useDef synchronize];
    [self.timer invalidate];
    self.timer = nil;
    // 切换根视图控制器
    self.view.window.rootViewController = [[TabBarViewController alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    NSLog(@"内存警告");
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    [self.timer invalidate];
    self.timer = nil;
    
}

/*停止拖动的时候调用*/

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    /*开启*/
    [self.timer setFireDate:[NSDate distantPast]];
    [self addTimer];
    if (self.myScrollView.contentOffset.x == KSCREEWIDTH) {
        self.pageControl.currentPage = 1;
        self.myScrollView.contentOffset = CGPointMake(KSCREEWIDTH*2, 0);
        return;
        
    }
    if (self.myScrollView.contentOffset.x == KSCREEWIDTH*2) {
        self.pageControl.currentPage = 2;
        self.myScrollView.contentOffset = CGPointMake(KSCREEWIDTH*3, 0);
        [self.timer invalidate];
        self.timer = nil;

        return;
        
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{
    
    NSInteger index = scrollView.contentOffset.x /scrollView.frame.size.width;
    
    self.pageControl.currentPage =index;
    
}


/*开启定时器*/

- (void)addTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(jump) userInfo:nil repeats:YES];
}

-(void)jump{
    NSLog(@"jump");
    if (self.myScrollView.contentOffset.x == 0) {
        self.pageControl.currentPage = 1;
        self.myScrollView.contentOffset = CGPointMake(KSCREEWIDTH, 0);
        return;
        
    }
    if (self.myScrollView.contentOffset.x == KSCREEWIDTH) {
        self.pageControl.currentPage = 2;
        self.myScrollView.contentOffset = CGPointMake(KSCREEWIDTH*2, 0);
        [self.timer invalidate];
        self.timer = nil;
        return;

    }
}


@end
